//
//  ZGLPicTableViewCell.m
//  Faker
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLPicTableViewCell.h"
#import "ZGLPicCollectionViewCell.h"
#import "ZGLPhotoModel.h"
#import "ZGLPicVCModel.h"

@interface ZGLPicTableViewCell () <UICollectionViewDataSource, UICollectionViewDelegate>


@property (nonatomic, strong) UICollectionView *picCollectionView;

@end

@implementation ZGLPicTableViewCell
- (void)dealloc {
    _picCollectionView.dataSource = nil;
    _picCollectionView.delegate = nil;
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(ScaleX * 180, ScaleY * 140);
    flow.scrollDirection = 1;
    flow.sectionInset = UIEdgeInsetsMake(ScaleX * 2.5, ScaleY * 5, ScaleX * 2.5, ScaleY * 2.5);
    flow.minimumInteritemSpacing = ScaleY * 2.5;
    flow.minimumLineSpacing = ScaleY * 5;
    _picCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScaleX * 375, ScaleY * 435) collectionViewLayout:flow];
    [self.contentView addSubview:_picCollectionView];
    _picCollectionView.delegate = self;
    _picCollectionView.dataSource = self;
    
    [_picCollectionView registerClass:[ZGLPicCollectionViewCell class] forCellWithReuseIdentifier:@"ZGLPicTableViewCell_ZGLPicCollectionViewCell"];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCollectionView];
    }
    return self;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    _picCollectionView.backgroundColor = foreColor;
}

- (void)setPicArr:(NSMutableArray *)picArr {
    if (_picArr != picArr) {
        _picArr = picArr;
    }
    
    [self.picCollectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (_picArr.count == 0) {
        return 2;
    } else {
        return _picArr.count;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZGLPicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZGLPicTableViewCell_ZGLPicCollectionViewCell" forIndexPath:indexPath];
    cell.picModel = _picArr[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  
    ZGLPicVCModel *model = _picArr[indexPath.item];
    NSMutableArray *modelArr = [NSMutableArray array];
    for (NSDictionary *dic in model.media) {
        ZGLPhotoModel *model = [ZGLPhotoModel modWithDic:dic];
        [modelArr addObject:model];
    }
    [self.delegate pass:modelArr];
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
